unit oFormLL;

{
	created   26.3.2010
	author   	Miroslav Mintal
	purpose
}

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls,
 Mathematic, Vector, RenderContext, View, Pedestrian, Obstacle, Path, uOSPRand, GL, GLu,
 Menus, Buttons, CheckLst, oProcessObjectConstants, oEntityTypeConstants,
 oProcessObjects, OProcessItem, oEditorManager, schodyFrm,
 oFrameLL, oFrameScene;


type
  TformLL = class(TForm)
    FrameLL1: TFrameLL;
    TfEditor1: TfEditor;
    procedure fLL1btnSaveClick(Sender: TObject);
    procedure fLL1btnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TfEditor1RenderPanelMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    function getFrame: TFrame;
    { Private declarations }
  public
    procedure init;
    property frame: TFrame read getFrame;
    { Public declarations }
  end;

var
  formLL: TformLL;

implementation

{$R *.dfm}

procedure TformLL.fLL1btnCancelClick(Sender: TObject);
begin
  FrameLL1.btnCancelClick(Sender);
  Close;
end;

procedure TformLL.fLL1btnSaveClick(Sender: TObject);
begin
  FrameLL1.btnSaveClick(Sender);
  if FrameLL1.Success then
    Close;
end;



procedure TformLL.FormCreate(Sender: TObject);
begin
 inherited;
 init;
end;

procedure TformLL.FormDestroy(Sender: TObject);
var I: Integer;
begin
  with TfEditor1 do begin
    RenderContext.Relase;
    RenderContext.Free;
    OrthogonalView.Free;
    for I := 0 to Pedestrians.Count -1 do begin
        TPedestrianEditor(Pedestrians[I]).Free;
    end;
    Pedestrians.Free;

    for I := 0 to Obstacles.Count -1 do begin
        TObstacle(Obstacles[I]).Free;
    end;
    Obstacles.Free;

    for I := 0 to LogObstacles.Count - 1 do begin
        TObstacle(LogObstacles[I]).Free;
    end;
    LogObstacles.Free;

    for I := 0 to Spawns.Count - 1 do begin
      TObstacle(Spawns[I]).Free;
    end;
    Spawns.Free;

    for I := 0 to Sinks.Count - 1 do begin
      TObstacle(Sinks[I]).Free;
    end;
    Sinks.Free;

    for I := 0 to PathPoints.Count - 1 do begin
      TObstacle(PathPoints[I]).Free;
    end;
    PathPoints.Free;

  end;
end;

function TformLL.getFrame: TFrame;
begin
  Result := FrameLL1;
end;

procedure TformLL.init;
var Pedestrian: TPedestrianEditor;
    I: Integer;
    pos : TVector;
begin
with TfEditor1 do begin
    Application.OnIdle :=  IdleLoop;
    ViewWidth := LevelSize;
    RenderContext := TRenderContext.Create;
    LogLayRBtn.Checked:= true;

    TransfBtn.Enabled:= true;

    PolyBtn.Enabled:= true;
    CircleBtn.Enabled:= false;
    SpawnBtn.Enabled:= true;
    SinkBtn.Enabled:= true;
    PathPointBtn.Enabled:= true;
    PathWayBtn.Enabled:= false;

    if not RenderContext.Init(RenderPanel.Handle, true) then Caption := 'RenderContext.Init Error';
//    OrthogonalView := TOrthogonalView.Create;
    pos := TVector.Create(LevelSize / 2, LevelSize / 2, 0);
    OrthogonalView := TOrthogonalView.Create(pos, ViewWidth, ViewWidth, 0);

    Pedestrians := TPedestriansList.Create;
    Obstacles := TObstaclesList.Create;
    LogObstacles:= TObstaclesList.Create;
    Spawns:= TObstaclesList.Create;
    Sinks:= TObstaclesList.Create;
    PathPoints:= TObstaclesList.Create;
    AllObstacles:= TObstaclesList.Create;
    Path:= TPath2.Create;

    PickedPedestrianIndex := -1;
    PickedObstacleIndex := -1;
    PickedSpawnIndex := -1;
    PickedSinkIndex := -1;
    PickedPathPointIndex:= -1;
    PickedPointIndex := -1;
    PickedPointIndex1 := -1;
    PickedPointIndex2 := -1;
    PickedPointIndex3 := -1;
    PolygonObstacle := nil;
    CircleObstacle := nil;
    R := 0.5; G := 0.5; B := 0.5;

    // nastavenie velkosti mriezky
    GridX := 1; GridY := 1;

    Start := false;
    // vytvorenie GL sceny
    InitGLScene;
    Randomize;
  end;
end;

procedure TformLL.TfEditor1RenderPanelMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  TfEditor1.RenderPanelMouseDown(Sender, Button, Shift, X, Y);
end;



end.
